/*
 * data.c
 *
 *  Created on: Mar 27, 2013
 *      Author: locpx
 */
#include "board.h"
#include "extglobals.h"
#include "protos.h"
#include "move.h"

void initData()
{
	int i, file, rank, arank, afile;
	//     ===========================================================================
	//     BITSET has only one bit set
	//     ===========================================================================
	BITSET[0] = 0x00000001;
	for (i = 1; i < 30 ; i++)
	{
		BITSET[i] = BITSET[i-1] << 1;
	}

	//     ===========================================================================
	//     BITBOARDSET has only one bit set
	//     ===========================================================================
	for (i = 0; i < 90 ; i++)
	{
		clearBitBoard(&BITBOARDSET[i]);
		BITBOARDSET[i].bb[SQUARE_TO_BITBOARD_INDEX[i]] = BITSET[SQUARE_TO_BITSET_INDEX[i]];
	}

	//     ===========================================================================
	//     BOARDINDEX is used to translate [file][rank] to [square],
	//     Note that file is from 0..8 and rank from 0..9
	//     ===========================================================================
	for (rank = 0 ; rank < 10; rank++)
	{
		for (file = 0 ; file < 9; file++)
		{
			BOARDINDEX[file][rank] = rank*9 + file;
		}
	}

	//     ===========================================================================
	//     Initialize all attack bitmaps, used in the move generator (see movegen.c)
	//     ===========================================================================
	int square;
	for (square = 0; square < 90; square++)
	{
		for (i = 0; i < 4; i++)
		{
			clearBitBoard(&HORSE_ATTACKS[square][i]);
			clearBitBoard(&HORSE_OBSTRUCTS[square][i]);
			clearBitBoard(&ELEPHANT_ATTACKS[square][i]);
			clearBitBoard(&ELEPHANT_OBSTRUCTS[square][i]);
		}
		clearBitBoard(&ADVISOR_ATTACKS[square]);
		clearBitBoard(&KING_ATTACKS[square]);
		clearBitBoard(&WHITE_PAWN_ATTACKS[square]);
		clearBitBoard(&BLACK_PAWN_ATTACKS[square]);
		clearBitBoard(&ATTACKS_RIGHT[square]);
		clearBitBoard(&ATTACKS_TOP[square]);
		clearBitBoard(&ATTACKS_LEFT[square]);
		clearBitBoard(&ATTACKS_BOTTOM[square]);
	}

	// HORSE attacks
	// [from][0] => RIGHT-UP, RIGHT-DOWN
	// [from][1] => LEFT-UP, LEFT-DOWN
	// [from][2] => UP-RIGHT, UP-LEFT
	// [from][3] => DOWN-RIGHT, DOWN-LEFT
	// HORSE obstructs
	// [from][0] => RIGHT
	// [from][1] => LEFT
	// [from][2] => UP
	// [from][3] => DOWN
	for (square = 0; square < 90; square++)
	{
		file = FILES[square];
		rank = RANKS[square];

		// [from][0] => RIGHT-UP, RIGHT-DOWN
		afile = file + 2; arank = rank + 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][0].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file + 2; arank = rank - 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][0].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		// [from][0] => RIGHT
		afile = file + 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_OBSTRUCTS[square][0].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];

		// [from][2] => LEFT-UP, LEFT-DOWN
		afile = file - 2; arank = rank + 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][1].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file - 2; arank = rank - 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][1].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		// [from][1] => LEFT
		afile = file - 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_OBSTRUCTS[square][1].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];

		// [from][4] => UP-RIGHT, UP-LEFT
		afile = file + 1; arank = rank + 2;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][2].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file - 1; arank = rank + 2;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][2].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		// [from][2] => UP
		afile = file; arank = rank + 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_OBSTRUCTS[square][2].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];

		// [from][6] => DOWN-RIGHT, DOWN-LEFT
		afile = file + 1; arank = rank - 2;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][3].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file - 1; arank = rank - 2;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_ATTACKS[square][3].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		// [from][3] => DOWN
		afile = file; arank = rank - 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			HORSE_OBSTRUCTS[square][3].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
	}

	// ELEPHANT attacks, obstructs
	// [from][0] => RIGHT-UP
	// [from][1] => RIGHT-DOWN
	// [from][2] => LEFT-UP
	// [from][3] => LEFT-DOWN
	ELEPHANT_ATTACKS[C0][0] = orTwoBitBoards(ELEPHANT_ATTACKS[C0][0], BITBOARDSET[A2]);
	ELEPHANT_ATTACKS[C0][2] = orTwoBitBoards(ELEPHANT_ATTACKS[C0][2], BITBOARDSET[E2]);
	ELEPHANT_OBSTRUCTS[C0][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C0][0], BITBOARDSET[B1]);
	ELEPHANT_OBSTRUCTS[C0][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C0][2], BITBOARDSET[D1]);

	ELEPHANT_ATTACKS[G0][0] = orTwoBitBoards(ELEPHANT_ATTACKS[G0][0], BITBOARDSET[E2]);
	ELEPHANT_ATTACKS[G0][2] = orTwoBitBoards(ELEPHANT_ATTACKS[G0][2], BITBOARDSET[I2]);
	ELEPHANT_OBSTRUCTS[G0][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G0][0], BITBOARDSET[F1]);
	ELEPHANT_OBSTRUCTS[G0][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G0][2], BITBOARDSET[H1]);

	ELEPHANT_ATTACKS[A2][0] = orTwoBitBoards(ELEPHANT_ATTACKS[A2][0], BITBOARDSET[C0]);
	ELEPHANT_ATTACKS[A2][1] = orTwoBitBoards(ELEPHANT_ATTACKS[A2][1], BITBOARDSET[C4]);
	ELEPHANT_OBSTRUCTS[A2][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[A2][0], BITBOARDSET[B1]);
	ELEPHANT_OBSTRUCTS[A2][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[A2][1], BITBOARDSET[B3]);


	ELEPHANT_ATTACKS[E2][0] = orTwoBitBoards(ELEPHANT_ATTACKS[E2][0], BITBOARDSET[C0]);
	ELEPHANT_ATTACKS[E2][1] = orTwoBitBoards(ELEPHANT_ATTACKS[E2][1], BITBOARDSET[G0]);
	ELEPHANT_ATTACKS[E2][2] = orTwoBitBoards(ELEPHANT_ATTACKS[E2][2], BITBOARDSET[C4]);
	ELEPHANT_ATTACKS[E2][3] = orTwoBitBoards(ELEPHANT_ATTACKS[E2][3], BITBOARDSET[G4]);
	ELEPHANT_OBSTRUCTS[E2][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E2][0], BITBOARDSET[D1]);
	ELEPHANT_OBSTRUCTS[E2][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E2][1], BITBOARDSET[F1]);
	ELEPHANT_OBSTRUCTS[E2][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E2][2], BITBOARDSET[D3]);
	ELEPHANT_OBSTRUCTS[E2][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E2][3], BITBOARDSET[F3]);

	ELEPHANT_ATTACKS[I2][2] = orTwoBitBoards(ELEPHANT_ATTACKS[I2][2], BITBOARDSET[G0]);
	ELEPHANT_ATTACKS[I2][3] = orTwoBitBoards(ELEPHANT_ATTACKS[I2][3], BITBOARDSET[G4]);
	ELEPHANT_OBSTRUCTS[I2][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[I2][2], BITBOARDSET[H1]);
	ELEPHANT_OBSTRUCTS[I2][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[I2][3], BITBOARDSET[H3]);

	ELEPHANT_ATTACKS[C4][1] = orTwoBitBoards(ELEPHANT_ATTACKS[C4][1], BITBOARDSET[A2]);
	ELEPHANT_ATTACKS[C4][3] = orTwoBitBoards(ELEPHANT_ATTACKS[C4][3], BITBOARDSET[E2]);
	ELEPHANT_OBSTRUCTS[C4][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C4][1], BITBOARDSET[B3]);
	ELEPHANT_OBSTRUCTS[C4][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C4][3], BITBOARDSET[D3]);

	ELEPHANT_ATTACKS[G4][1] = orTwoBitBoards(ELEPHANT_ATTACKS[G4][1], BITBOARDSET[E2]);
	ELEPHANT_ATTACKS[G4][3] = orTwoBitBoards(ELEPHANT_ATTACKS[G4][3], BITBOARDSET[I2]);
	ELEPHANT_OBSTRUCTS[G4][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G4][1], BITBOARDSET[F3]);
	ELEPHANT_OBSTRUCTS[G4][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G4][3], BITBOARDSET[H3]);

	ELEPHANT_ATTACKS[C9][1] = orTwoBitBoards(ELEPHANT_ATTACKS[C9][1], BITBOARDSET[A7]);
	ELEPHANT_ATTACKS[C9][3] = orTwoBitBoards(ELEPHANT_ATTACKS[C9][3], BITBOARDSET[E7]);
	ELEPHANT_OBSTRUCTS[C9][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C9][1], BITBOARDSET[B8]);
	ELEPHANT_OBSTRUCTS[C9][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C9][3], BITBOARDSET[D8]);

	ELEPHANT_ATTACKS[G9][1] = orTwoBitBoards(ELEPHANT_ATTACKS[G9][1], BITBOARDSET[E7]);
	ELEPHANT_ATTACKS[G9][3] = orTwoBitBoards(ELEPHANT_ATTACKS[G9][3], BITBOARDSET[I7]);
	ELEPHANT_OBSTRUCTS[G9][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G9][1], BITBOARDSET[F8]);
	ELEPHANT_OBSTRUCTS[G9][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G9][3], BITBOARDSET[H8]);

	ELEPHANT_ATTACKS[A7][0] = orTwoBitBoards(ELEPHANT_ATTACKS[A7][0], BITBOARDSET[C9]);
	ELEPHANT_ATTACKS[A7][1] = orTwoBitBoards(ELEPHANT_ATTACKS[A7][1], BITBOARDSET[C5]);
	ELEPHANT_OBSTRUCTS[A7][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[A7][0], BITBOARDSET[B8]);
	ELEPHANT_OBSTRUCTS[A7][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[A7][1], BITBOARDSET[B6]);

	ELEPHANT_ATTACKS[E7][0] = orTwoBitBoards(ELEPHANT_ATTACKS[E7][0], BITBOARDSET[C9]);
	ELEPHANT_ATTACKS[E7][1] = orTwoBitBoards(ELEPHANT_ATTACKS[E7][1], BITBOARDSET[G9]);
	ELEPHANT_ATTACKS[E7][2] = orTwoBitBoards(ELEPHANT_ATTACKS[E7][2], BITBOARDSET[C5]);
	ELEPHANT_ATTACKS[E7][3] = orTwoBitBoards(ELEPHANT_ATTACKS[E7][3], BITBOARDSET[G5]);
	ELEPHANT_OBSTRUCTS[E7][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E7][0], BITBOARDSET[D8]);
	ELEPHANT_OBSTRUCTS[E7][1] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E7][1], BITBOARDSET[F8]);
	ELEPHANT_OBSTRUCTS[E7][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E7][2], BITBOARDSET[D6]);
	ELEPHANT_OBSTRUCTS[E7][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[E7][3], BITBOARDSET[F6]);

	ELEPHANT_ATTACKS[I7][2] = orTwoBitBoards(ELEPHANT_ATTACKS[I7][2], BITBOARDSET[G9]);
	ELEPHANT_ATTACKS[I7][3] = orTwoBitBoards(ELEPHANT_ATTACKS[I7][3], BITBOARDSET[G5]);
	ELEPHANT_OBSTRUCTS[I7][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[I7][2], BITBOARDSET[H8]);
	ELEPHANT_OBSTRUCTS[I7][3] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[I7][3], BITBOARDSET[H6]);

	ELEPHANT_ATTACKS[C5][0] = orTwoBitBoards(ELEPHANT_ATTACKS[C5][0], BITBOARDSET[A7]);
	ELEPHANT_ATTACKS[C5][2] = orTwoBitBoards(ELEPHANT_ATTACKS[C5][2], BITBOARDSET[E7]);
	ELEPHANT_OBSTRUCTS[C5][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C5][0], BITBOARDSET[B6]);
	ELEPHANT_OBSTRUCTS[C5][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[C5][2], BITBOARDSET[D6]);

	ELEPHANT_ATTACKS[G5][0] = orTwoBitBoards(ELEPHANT_ATTACKS[G5][0], BITBOARDSET[E7]);
	ELEPHANT_ATTACKS[G5][2] = orTwoBitBoards(ELEPHANT_ATTACKS[G5][2], BITBOARDSET[I7]);
	ELEPHANT_OBSTRUCTS[G5][0] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G5][0], BITBOARDSET[F6]);
	ELEPHANT_OBSTRUCTS[G5][2] = orTwoBitBoards(ELEPHANT_OBSTRUCTS[G5][2], BITBOARDSET[H6]);

	// ADVISOR attacks
	ADVISOR_ATTACKS[D0] = orTwoBitBoards(ADVISOR_ATTACKS[D0], BITBOARDSET[E1]);
	ADVISOR_ATTACKS[F0] = orTwoBitBoards(ADVISOR_ATTACKS[F0], BITBOARDSET[E1]);
	ADVISOR_ATTACKS[D2] = orTwoBitBoards(ADVISOR_ATTACKS[D2], BITBOARDSET[E1]);
	ADVISOR_ATTACKS[F2] = orTwoBitBoards(ADVISOR_ATTACKS[F2], BITBOARDSET[E1]);
	BITBOARD whiteAdvisorMoves[] = {BITBOARDSET[D0], BITBOARDSET[D2], BITBOARDSET[F0], BITBOARDSET[F2]};
	ADVISOR_ATTACKS[E1] = orMultipleBitBoards(whiteAdvisorMoves, 4);

	ADVISOR_ATTACKS[D9] = orTwoBitBoards(ADVISOR_ATTACKS[D9], BITBOARDSET[E8]);
	ADVISOR_ATTACKS[F9] = orTwoBitBoards(ADVISOR_ATTACKS[F9], BITBOARDSET[E8]);
	ADVISOR_ATTACKS[D2] = orTwoBitBoards(ADVISOR_ATTACKS[D2], BITBOARDSET[E8]);
	ADVISOR_ATTACKS[F2] = orTwoBitBoards(ADVISOR_ATTACKS[F2], BITBOARDSET[E8]);
	BITBOARD blackAdvisorMoves[] = {BITBOARDSET[D9], BITBOARDSET[D7], BITBOARDSET[F9], BITBOARDSET[F7]};
	ADVISOR_ATTACKS[E8] = orMultipleBitBoards(blackAdvisorMoves, 4);

	// KING attacks
	KING_ATTACKS[D0] = orTwoBitBoards(BITBOARDSET[E0], BITBOARDSET[D1]);
	BITBOARD whiteKingMovesAtE0[] = {BITBOARDSET[D0], BITBOARDSET[F0], BITBOARDSET[E1]};
	KING_ATTACKS[E0] = orMultipleBitBoards(whiteKingMovesAtE0, 3);
	KING_ATTACKS[F0] = orTwoBitBoards(BITBOARDSET[E0], BITBOARDSET[F1]);

	BITBOARD whiteKingMovesAtD1[] = {BITBOARDSET[D0], BITBOARDSET[D2], BITBOARDSET[E1]};
	KING_ATTACKS[D1] = orMultipleBitBoards(whiteKingMovesAtD1, 3);
	BITBOARD whiteKingMovesAtE1[] = {BITBOARDSET[D1], BITBOARDSET[F1], BITBOARDSET[E0], BITBOARDSET[E2]};
	KING_ATTACKS[E1] = orMultipleBitBoards(whiteKingMovesAtE1, 4);
	BITBOARD whiteKingMovesAtF1[] = {BITBOARDSET[F0], BITBOARDSET[F2], BITBOARDSET[E1]};
	KING_ATTACKS[F1] = orMultipleBitBoards(whiteKingMovesAtF1, 3);

	KING_ATTACKS[D2] = orTwoBitBoards(BITBOARDSET[E2], BITBOARDSET[D1]);
	BITBOARD whiteKingMovesAtE2[] = {BITBOARDSET[D2], BITBOARDSET[F2], BITBOARDSET[E1]};
	KING_ATTACKS[E2] = orMultipleBitBoards(whiteKingMovesAtE2, 3);
	KING_ATTACKS[F2] = orTwoBitBoards(BITBOARDSET[E2], BITBOARDSET[F1]);

	//.................................................................................//

	KING_ATTACKS[D9] = orTwoBitBoards(BITBOARDSET[E9], BITBOARDSET[D8]);
	BITBOARD blackKingMovesAtE9[] = {BITBOARDSET[D9], BITBOARDSET[F9], BITBOARDSET[E8]};
	KING_ATTACKS[E9] = orMultipleBitBoards(blackKingMovesAtE9, 3);
	KING_ATTACKS[F9] = orTwoBitBoards(BITBOARDSET[E9], BITBOARDSET[F8]);

	BITBOARD blackKingMovesAtD8[] = {BITBOARDSET[D9], BITBOARDSET[D7], BITBOARDSET[E8]};
	KING_ATTACKS[D8] = orMultipleBitBoards(blackKingMovesAtD8, 3);
	BITBOARD blackKingMovesAtE8[] = {BITBOARDSET[D8], BITBOARDSET[F8], BITBOARDSET[E9], BITBOARDSET[E7]};
	KING_ATTACKS[E8] = orMultipleBitBoards(blackKingMovesAtE8, 4);
	BITBOARD blackKingMovesAtF8[] = {BITBOARDSET[F9], BITBOARDSET[F7], BITBOARDSET[E8]};
	KING_ATTACKS[F8] = orMultipleBitBoards(blackKingMovesAtF8, 3);

	KING_ATTACKS[D7] = orTwoBitBoards(BITBOARDSET[E7], BITBOARDSET[D8]);
	BITBOARD blackKingMovesAtE7[] = {BITBOARDSET[D7], BITBOARDSET[F7], BITBOARDSET[E8]};
	KING_ATTACKS[E7] = orMultipleBitBoards(blackKingMovesAtE7, 3);
	KING_ATTACKS[F7] = orTwoBitBoards(BITBOARDSET[E7], BITBOARDSET[F8]);

	// White PAWN attacks
	WHITE_PAWN_ATTACKS[A3] = orTwoBitBoards(WHITE_PAWN_ATTACKS[A3], BITBOARDSET[A4]);
	WHITE_PAWN_ATTACKS[C3] = orTwoBitBoards(WHITE_PAWN_ATTACKS[C3], BITBOARDSET[C4]);
	WHITE_PAWN_ATTACKS[E3] = orTwoBitBoards(WHITE_PAWN_ATTACKS[E3], BITBOARDSET[E4]);
	WHITE_PAWN_ATTACKS[G3] = orTwoBitBoards(WHITE_PAWN_ATTACKS[G3], BITBOARDSET[G4]);
	WHITE_PAWN_ATTACKS[I3] = orTwoBitBoards(WHITE_PAWN_ATTACKS[I3], BITBOARDSET[I4]);

	WHITE_PAWN_ATTACKS[A4] = orTwoBitBoards(WHITE_PAWN_ATTACKS[A4], BITBOARDSET[A5]);
	WHITE_PAWN_ATTACKS[C4] = orTwoBitBoards(WHITE_PAWN_ATTACKS[C4], BITBOARDSET[C5]);
	WHITE_PAWN_ATTACKS[E4] = orTwoBitBoards(WHITE_PAWN_ATTACKS[E4], BITBOARDSET[E5]);
	WHITE_PAWN_ATTACKS[G4] = orTwoBitBoards(WHITE_PAWN_ATTACKS[G4], BITBOARDSET[G5]);
	WHITE_PAWN_ATTACKS[I4] = orTwoBitBoards(WHITE_PAWN_ATTACKS[I4], BITBOARDSET[I5]);

	for (square = A5; square <= I9; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		afile = file - 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			WHITE_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file + 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			WHITE_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file; arank = rank + 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			WHITE_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
	}

	BLACK_PAWN_ATTACKS[A6] = orTwoBitBoards(BLACK_PAWN_ATTACKS[A6], BITBOARDSET[A5]);
	BLACK_PAWN_ATTACKS[C6] = orTwoBitBoards(BLACK_PAWN_ATTACKS[C6], BITBOARDSET[C5]);
	BLACK_PAWN_ATTACKS[E6] = orTwoBitBoards(BLACK_PAWN_ATTACKS[E6], BITBOARDSET[E5]);
	BLACK_PAWN_ATTACKS[G6] = orTwoBitBoards(BLACK_PAWN_ATTACKS[G6], BITBOARDSET[G5]);
	BLACK_PAWN_ATTACKS[I6] = orTwoBitBoards(BLACK_PAWN_ATTACKS[I6], BITBOARDSET[I5]);

	BLACK_PAWN_ATTACKS[A5] = orTwoBitBoards(BLACK_PAWN_ATTACKS[A5], BITBOARDSET[A4]);
	BLACK_PAWN_ATTACKS[C5] = orTwoBitBoards(BLACK_PAWN_ATTACKS[C5], BITBOARDSET[C4]);
	BLACK_PAWN_ATTACKS[E5] = orTwoBitBoards(BLACK_PAWN_ATTACKS[E5], BITBOARDSET[E4]);
	BLACK_PAWN_ATTACKS[G5] = orTwoBitBoards(BLACK_PAWN_ATTACKS[G5], BITBOARDSET[G4]);
	BLACK_PAWN_ATTACKS[I5] = orTwoBitBoards(BLACK_PAWN_ATTACKS[I5], BITBOARDSET[I4]);

	for (square = A0; square <= I4; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		afile = file - 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			BLACK_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file + 1; arank = rank;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			BLACK_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
		afile = file; arank = rank - 1;
		if ((afile >= 0) & (afile <= 8) & (arank >= 0) & (arank <= 9))
			BLACK_PAWN_ATTACKS[square].bb[SQUARE_TO_BITBOARD_INDEX[BOARDINDEX[afile][arank]]] |= BITSET[SQUARE_TO_BITSET_INDEX[BOARDINDEX[afile][arank]]];
	}

	//     ===========================================================================
	//     Initialize ROOK_ATTACKS_RIGHT
	//     ===========================================================================
	for (square = 0; square < 90; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		afile = file + 1;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 2;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 3;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 4;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 5;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 6;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 7;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file + 8;
		if (afile < 9)
			ATTACKS_RIGHT[square] = orTwoBitBoards(ATTACKS_RIGHT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
	}

	//     ===========================================================================
	//     Initialize ROOK_ATTACKS_TOP
	//     ===========================================================================
	for (square = 0; square < 90; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		arank = rank + 1;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 2;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 3;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 4;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 5;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 6;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 7;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 8;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank + 9;
		if (arank < 10)
			ATTACKS_TOP[square] = orTwoBitBoards(ATTACKS_TOP[square], BITBOARDSET[BOARDINDEX[file][arank]]);
	}

	//     ===========================================================================
	//     Initialize ROOK_ATTACKS_LEFT
	//     ===========================================================================
	for (square = 0; square < 90; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		afile = file - 1;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 2;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 3;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 4;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 5;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 6;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 7;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
		afile = file - 8;
		if (afile >= 0)
			ATTACKS_LEFT[square] = orTwoBitBoards(ATTACKS_LEFT[square], BITBOARDSET[BOARDINDEX[afile][rank]]);
	}

	//     ===========================================================================
	//     Initialize ROOK_ATTACKS_BOTTOM
	//     ===========================================================================
	for (square = 0; square < 90; square++)
	{
		file = FILES[square];
		rank = RANKS[square];
		arank = rank - 1;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 2;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 3;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 4;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 5;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 6;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 7;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 8;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
		arank = rank - 9;
		if (arank >= 0)
			ATTACKS_BOTTOM[square] = orTwoBitBoards(ATTACKS_BOTTOM[square], BITBOARDSET[BOARDINDEX[file][arank]]);
	}

	//     ===========================================================================
	//     Initialize the board
	//     ===========================================================================
	initBoard(&board);

	clear(&NOMOVE);
	return;
}
